package com.ja.openchat.viewmodels;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableField;

import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.model.user.UserInfo;

import me.goldze.mvvmhabit.base.MultiItemViewModel;
import me.goldze.mvvmhabit.binding.command.BindingAction;
import me.goldze.mvvmhabit.binding.command.BindingCommand;

public class MemberItemViewModel extends MultiItemViewModel<ChatSearchPageViewModel> {
    public ObservableField<UserInfo> entity = new ObservableField<>();
    public ObservableField<String> name = new ObservableField<>();

    public ObservableField<UserInfo> userAvatar = new ObservableField<>();
    public MemberItemViewModel(@NonNull ChatSearchPageViewModel viewModel, UserInfo entity) {
        super(viewModel);
        this.entity.set(entity);
        update(entity);
    }

    private void update(UserInfo entity) {
        UserInfo userInfo = UserInfo.getUserInfo(entity.id);
        if (userInfo != null) {
            name.set(userInfo.displayName());
            userAvatar.set(userInfo);
            userAvatar.notifyChange();
        } else {
            name.set("");
            TelegramManager.getTelegramManager().getUserSimpleInfo(entity.id);
        }

    }

    /**
     * 获取position的方式有很多种,indexOf是其中一种，常见的还有在Adapter中、ItemBinding.of回调里
     *
     * @return
     */
//    public int getPosition() {
//        return viewModel.getItemPosition(this);
//    }

    //条目的点击事件
    public BindingCommand itemClick = new BindingCommand(new BindingAction() {
        @Override
        public void call() {
//            Bundle bundle = new Bundle();
//            bundle.putLong("userId", entity.get().user_id);
//            viewModel.startContainerActivity(MyAlbumFragment.class.getCanonicalName(), bundle);
        }
    });
}
